package com.mengchengnet.chaperonage.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/15.
 * 意见反馈
 */

public class FeedBackActivity extends BaseActivity {
    private EditText ed_tips;
    private Button btn_sure;
    private RequestQueue requestQueue;
    private String userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initView();
    }

    private void initView() {
        userId=PreferenceUtil.getString(getApplicationContext(),"userId");
        requestQueue= Volley.newRequestQueue(getApplication());
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("意见反馈");
        ed_tips= (EditText) findViewById(R.id.feedback_ed_tips);
        btn_sure= (Button) findViewById(R.id.feedback_btn_sure);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              if (!ed_tips.getContext().toString().isEmpty()){
                  if (!userId.isEmpty()){
                      feedBack();
                  }

              }else {
                  Toast.makeText(FeedBackActivity.this, "请输入您的意见和建议", Toast.LENGTH_SHORT).show();
              }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void feedBack(){
        StringRequest request = new StringRequest(Request.Method.POST, Url.FeedBack,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("意见反馈", "意见反馈接口返回值："+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                Toast.makeText(FeedBackActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }else if(jsonObject.getString("code").equals("01")){
                                Toast.makeText(FeedBackActivity.this, "请输入您的意见和建议", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(FeedBackActivity.this, "提交失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(FeedBackActivity.this, "网络异常请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("version", PreferenceUtil.getString(getApplication(),"versionCode"));
                map.put("loginId", userId);
                Log.e("反馈", "反馈id参数: "+userId);
                map.put("feedback", ed_tips.getText().toString());
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }
}
